<?php
/**
 * Created by PhpStorm.
 * User: 丶晓
 * Date: 2017/9/20
 * Time: 9:56
 */

namespace app\api\model;


use think\Db;
use think\Model;

class DataModel extends Model
{
    // 查
    public static function selectInfo($type, $data = [])
    {
        if (empty($type)) {
            return false;
        }
        switch ($type) {
            case 'get_client_id':
                return Db::connect('mysql://root:suyongkeji@suyongw.cn:3306/suyongkeji#utf8')->table('wyz_miniapps')->where('id', $data['id'])->value('socket_id');
                break;
            case 'get_ticket':
                return Db::table('wyz_ticket')->where('id', 'eq', 1)->value('component_verify_ticket');
                break;
            case 'authorizer_appid':
                if (empty($data['id'])) {
                    return null;
                }
                return Db::table('wyz_miniapps')->where('id', $data['id'])->value('authorizer_appid');
                break;
            case 'authorizer_refresh_token':
                if (empty($data['id'])) {
                    return null;
                }
                return Db::table('wyz_miniapps')->where('id', $data['id'])->value('authorizer_refresh_token');
                break;
            case 'authorizer_access_token':
                if (empty($data['id'])) {
                    return null;
                }
                return Db::table('wyz_miniapps')->where('id', $data['id'])->value('authorizer_access_token');
                break;
            case 'appid_miniID': // miniID
                return Db::table('wyz_miniapps')->where('id', $data['id'])->value('AppId');
                break;
            case 'miniapp_pay_file': // 小程序支付所需资料
                $page = Db::table('wyz_cert')->where('miniapp_id', $data['id'])->count() ?
                    Db::table('wyz_cert')->alias('cert')
                        ->field([
                            'apps.AppId' => 'miniapp_id',
                            'cert.mch_id' => 'mch_id',
                            'cert.api' => 'key',
                            'cert.wx_cert' => 'cert_client',
                            'cert.wx_key' => 'cert_key',
                        ])
                        ->join('wyz_miniapps apps', 'cert.miniapp_id = apps.id', 'LEFT')
                        ->where('cert.miniapp_id', 'eq', $data['id'])->select() : [];
                return (!isset($page[0]['miniapp_id']) or empty($page[0]['miniapp_id'])) ? false : $page[0];
                break;
        }
        return false;
    }

    // 增
    public static function addInfo($type, $data = [])
    {
        if (empty($type) or empty($data)) {
            return false;
        }
        switch ($type) {
            case 'userMessage':
                $save = [
                    'byAppid' => $data['appid'],
                    'toName' => $data['appname'],
                    'openID' => $data['openid'],
                    'msgType' => $data['type'],
                    'Content' => $data['content'],
                    'msgId' => $data['msgid']
                ];
                if (Db::table('wyz_talk_to_user')->insert($save)) {
                    return true;
                }
                break;
        }
        return false;
    }

    // 改
    public static function changeInfo($type, $data = [])
    {
        if (empty($type)) {
            return false;
        }
        switch ($type) {
            case 'all_authorizer':
                if (empty($data['authorizer_refresh_token']) or empty($data['id'])) {
                    return false;
                }
                if (Db::table('wyz_miniapps')->where('id', $data['id'])->update([
                        'authorizer_access_token' => $data['authorizer_access_token'],
                        'authorizer_refresh_token' => $data['authorizer_refresh_token']
                    ]) == 1) {
                    return true;
                }
                break;
            case 'open_ticket':
                if (empty($data)) {
                    return false;
                }
                if (Db::table('wyz_ticket')->where('id', 1)->update($data) == 1) {
                    return true;
                }
                break;
            case 'miniapp_socket_id_ip':
                if (empty($data['miniapp_id']) or empty($data['id']) or empty($data['ip'])) {
                    return false;
                }
                if (Db::connect('mysql://root:suyongkeji@suyongw.cn:3306/suyongkeji#utf8')->table('wyz_miniapps')->where('id', 'eq', $data['miniapp_id'])->update(['socket_id' => $data['id'],'socket_ip'=>$data['ip']]) == 1) {
                    return true;
                }
                break;
            case 'miniapp_socket_id':
                if (empty($data['ip']) or empty($data['id'])) {
                    return false;
                }

                if (Db::connect('mysql://root:suyongkeji@suyongw.cn:3306/suyongkeji#utf8')->table('wyz_miniapps')->where('socket_ip', 'eq', $data['ip'])->update(['socket_id' => $data['id']]) == 1) {
                    return true;
                }
                break;
        }
        return false;
    }

    // 删
    public static function deleteInfo($type, $data = [])
    {
        if (empty($type)) {
            return false;
        }
        switch ($type) {
            case 'miniapp_socket_offline':
                if (empty($data['client_id'])) {
                    return false;
                }
                return Db::connect('mysql://root:suyongkeji@suyongw.cn:3306/suyongkeji#utf8')->table('wyz_miniapps')->where('socket_id', 'eq', $data['client_id'])->update(['socket_id' => NULL]);
                break;
        }
        return false;

    }
}